let ownerId = 8;
let userId = 1;
//文件上传
$('.btn-select-file').click(function () {
    $('#houseFile').click();
    console.log($('#houseFile').val());
});
//文件资源管理器点击时触发的事件
function setBlogrollImageName(basicfile){
    console.log(basicfile.value);
    $('.house-img-td').append("<span style='font-weight: bolder; margin: 10px'>已选择文件:</span style='font-weight: bolder'><u>" + basicfile.value + "</u>");
}

//发布房源
$("#release-btn").click(function(){
    let img = "";
    let myForm = new FormData();
    myForm.append("img",  $("[name='house_image']")[0].files[0]);
    $.ajax({
        url: "../house/fileupload",    //请求的url地址
        type: "POST",
        data: myForm,
        async: false,
        contentType: false,
        processData: false,
        success:function(req){
            if (req.code == 200) {
                img = req.data;
            }else {
                alert(req.data);
            }
        },
    });
    //将release页面上的数据封装成house对象并返回json数据
    let house = getHouse();
    house.houseImage = img;
    let houseJson = JSON.stringify(house);
    console.log(houseJson);
    //上传房源
    $.ajax({
        url: "../house?owner_id=" + ownerId,    //请求的url地址
        type: "POST",
        data: houseJson,
        dataType: "json",   //返回格式为json
        contentType: "application/json;charset=UTF-8",
        async: false,   //请求是否异步，默认为异步，这也是ajax重要特性
        success:function(req){
            if (req.code == 200) {
                alert("处理成功 ");
            }else {
                alert(req.data);
            }
        }
    });
});

//展示我的房源
$("#nav-my-house").click(function(){
    $.ajax({
        url: "../house/findAll?user_id=" + userId,    //请求的url地址
        dataType: "json",   //返回格式为json
        contentType: "application/json;charset=UTF-8",
        async: true,//请求是否异步，默认为异步，这也是ajax重要特性
        type: "GET",   //请求方式
        success:function(req){
            //展示my-house界面
            showMyHouse(req);
        },
    });
});


//解决js动态添加的新节点class不生效，点击事件无效
$(document).on("click", ".house-detail", function () {
    let houseId = $(this).parent().parent().attr("value");
    console.log("houseId = ", houseId);
    window.location.replace("../html/house-detail.html?house_id=" + houseId);
});

//解决js动态添加的新节点class不生效，点击事件无效
$(document).on("click", ".house-deleted", function () {
    let houseId = $(this).parent().parent().attr("value");
    let thisTr = $(this).parent().parent();
    $.ajax({
        url: "../house?house_id=" + houseId,    //请求的url地址
        dataType: "json",   //返回格式为json
        contentType: "application/json;charset=UTF-8",
        async: true,//请求是否异步，默认为异步，这也是ajax重要特性
        type:"DELETE",
        dataType:"json",
        success:function(req){
            if (req.code == 200) {
                //删除行
                thisTr.remove();
                alert("删除成功!");
            }
        }
    });
});

$('#nav-my-order').click(function () {
    $.ajax({
        url: "../order/findAll?owner_id=" + ownerId + "&order_status=1",    //请求的url地址
        dataType: "json",   //返回格式为json
        contentType: "application/json;charset=UTF-8",
        async: true,//请求是否异步，默认为异步，这也是ajax重要特性
        type: "GET",   //请求方式
        success:function(req){
            //展示my-house界面
            showMyOrder(req);
        },
    });
});

$('#nav-rent-out-message').click(function () {
    $.ajax({
        url: "../order/rent_out_message?owner_id=" + ownerId + "&order_status=0",    //请求的url地址
        dataType: "json",   //返回格式为json
        contentType: "application/json;charset=UTF-8",
        async: true,//请求是否异步，默认为异步，这也是ajax重要特性
        type: "GET",   //请求方式
        success:function(req){
            //展示my-house界面
            showRentOutMessage(req);
        },
    });
});

$('#nav-my-evaluation').click(function () {
    $.ajax({
        url: "../evaluation/findAll?owner_id=" + ownerId,    //请求的url地址
        dataType: "json",   //返回格式为json
        contentType: "application/json;charset=UTF-8",
        async: true,//请求是否异步，默认为异步，这也是ajax重要特性
        type: "GET",   //请求方式
        success:function(req){
            //展示my-house界面
            showMyEvaluation(req);
        },
    });
});

//解决js动态添加的新节点class不生效，点击事件无效
$(document).on("click", ".order-deleted", function () {
    let orderId = $(this).parent().parent().attr("value");
    let thisTr = $(this).parent().parent();
    $.ajax({
        url: "../order?order_id=" + orderId,    //请求的url地址
        dataType: "json",   //返回格式为json
        contentType: "application/json;charset=UTF-8",
        async: true,//请求是否异步，默认为异步，这也是ajax重要特性
        type:"DELETE",
        dataType:"json",
        success:function(req){
            if (req.code == 200) {
                //删除行
                thisTr.remove();
                alert("删除成功!");
            }
        }
    });
});

//解决js动态添加的新节点class不生效，点击事件无效
$(document).on("click", ".evaluation-deleted", function () {
    let evaluationId = $(this).parent().parent().attr("value");
    let thisTr = $(this).parent().parent();
    $.ajax({
        url: "../evaluation?evaluate_id=" + evaluationId,    //请求的url地址
        dataType: "json",   //返回格式为json
        contentType: "application/json;charset=UTF-8",
        async: true,//请求是否异步，默认为异步，这也是ajax重要特性
        type:"DELETE",
        dataType:"json",
        success:function(req){
            if (req.code == 200) {
                //删除行
                thisTr.remove();
                alert("删除成功!");
            }
        }
    });
});

//解决js动态添加的新节点class不生效，点击事件无效
$(document).on("click", ".rent-out-confirm", function () {
    let orderId = $(this).parent().parent().attr("value");
    let thisTr = $(this).parent().parent();
    $.ajax({
        url: "../order?order_id=" + orderId,    //请求的url地址
        dataType: "json",   //返回格式为json
        contentType: "application/json;charset=UTF-8",
        async: true,//请求是否异步，默认为异步，这也是ajax重要特性
        type:"PUT",
        dataType:"json",
        success:function(req){
            if (req.code == 200) {
                thisTr.remove();
                alert("出租成功!");
            }
        }
    });
});